package com.antis.olsl.user.login;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.antis.olsl.arouterlib.AppRoutePath;
import com.antis.olsl.arouterlib.UserRoutePath;
import com.antis.olsl.base.BaseActivity;
import com.antis.olsl.base.utils.LogUtils;
import com.antis.olsl.base.utils.ToastUtil;
import com.antis.olsl.library.MyApplication;
import com.antis.olsl.library.bean.UserInfo;
import com.antis.olsl.user.databinding.ActivityLoginBinding;
import com.antis.olsl.user.response.LoginResp;
import com.trello.rxlifecycle2.LifecycleTransformer;

import java.util.HashMap;

/**
 * @ProjectName: ATS_OLSL
 * @Package: com.ats.olsl.activity.login
 * @ClassName: LoginActivity
 * @Description: java类作用描述
 * @Author: 羊琪
 * @CreateDate: 2020/3/16 19:12
 * @UpdateDate: 2020/3/16 19:12
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Route(path = UserRoutePath.ACTIVITY_LOGIN)
public class LoginActivity extends BaseActivity<ActivityLoginBinding> implements LoginContract.View {

    private boolean isSee = false;
    private LoginPresenter mPresenter;

    @Override
    protected ActivityLoginBinding getViewBinding() {
        return ActivityLoginBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    protected void initPresenter() {
        super.initPresenter();
        mPresenter = new LoginPresenter();
        mPresenter.takeView(this);
    }

    @Override
    protected void initViews() {
        if (MyApplication.getUserInfo() != null) {
            ARouter.getInstance()
                    .build(AppRoutePath.ACTIVITY_ALL_FUNCTION)
//                    .build(AppRoutePath.ACTIVITY_TEST)
                    .withInt("selectFunctionIndex", 1)
                    .navigation();
            finish();
            return;
        }

        checkClickAble();
        viewBinding.etAccount.addTextChangedListener(new MyTextWatch());
        viewBinding.etPassword.addTextChangedListener(new MyTextWatch());

        viewBinding.imgSee.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changePasswordSee();
            }
        });
        viewBinding.tvForget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // readyGo(InputAccountActivity.class);
            }
        });
    }

    @Override
    protected void initData() throws IllegalAccessException {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mPresenter) {
            mPresenter.unSubscribe();
        }
    }

    private void checkClickAble() {
        String account = viewBinding.etAccount.getText().toString().trim();
        String password = viewBinding.etPassword.getText().toString().trim();
        viewBinding.tvLogin.setEnabled(!TextUtils.isEmpty(account) && !TextUtils.isEmpty(password));
    }

    private void changePasswordSee() {
        if (isSee) {
            viewBinding.etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            viewBinding.etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
        viewBinding.etPassword.setSelection(viewBinding.etPassword.getText().length());
        isSee = !isSee;
        viewBinding.imgSee.setSelected(isSee);
    }

    public void login(View view) {
        loginRequest();
    }

    private void loginRequest() {
        String account = viewBinding.etAccount.getText().toString().trim();
        String password = viewBinding.etPassword.getText().toString().trim();
        if (TextUtils.isEmpty(account)) {
            ToastUtil.showToast(this, "请输入账号");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtil.showToast(this, "请输入密码");
            return;
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("loginName", account);
        map.put("passWord", password);
        map.put("type", "2");
        map.put("clientId", "clientId");
        map.put("clientSecret", "clientSecret");
        mPresenter.login(map);
    }

    @Override
    public void loginSuccess(LoginResp resp) {
        UserInfo userInfo = resp.getContent();
        userInfo.setMerchantId(resp.getMerchantId());
        userInfo.setUserId(resp.getUserId());
        MyApplication.setUserInfo(resp.getContent());
        ToastUtil.showToast(this, "登录成功！");
        ARouter.getInstance()
                .build(AppRoutePath.ACTIVITY_ALL_FUNCTION)
//                    .build(AppRoutePath.ACTIVITY_TEST)
                .withInt("selectFunctionIndex", 1)
                .navigation();
        finish();
    }

    @Override
    public void loginFailure(String msg) {
        LogUtils.e("loginFailure msg= " + msg);
        if (TextUtils.isEmpty(msg)) {
            msg = "请求失败,请稍后重试";
        }
        ToastUtil.showToast(this, msg);
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.bindToLifecycle();
    }

    private class MyTextWatch implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            checkClickAble();
        }
    }
}
